/****************************************************************************************
 * Copyright (c) 2010~2012 All Rights Reserved by
 * G-Net Integrated Service Co., Ltd. 
 ****************************************************************************************/
package com.kingtone.rongji.adapter;

import java.util.ArrayList;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.kingtone.rongji.R;
import com.kingtone.rongji.biz.PromotionCode;
import com.kingtone.rongji.biz.PromotionCodeItem;
import com.kingtone.rongji.tool.RongJiUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

/**
 * @Title: MyCouponAdapter.java
 * @author: wenhui.li
 * @date: 2015-4-14 下午7:06:26
 */
public class MyCouponAdapter extends BaseAdapter
{
	private Context context = null;
	private ArrayList<PromotionCode> couponList = null;
	private LayoutInflater inflater;
	private ViewHolder holder = null;
	
	public MyCouponAdapter(Context context, ArrayList<PromotionCode> couponList)
	{
		this.inflater = LayoutInflater.from(context);
		this.context = context;
		this.couponList = couponList;
	}

	@Override
	public int getCount()
	{
		return couponList.size();
	}

	@Override
	public Object getItem(int position)
	{
		return couponList.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View view;
		// 如果已加载过，使用缓存技术不再重新加载
		if (convertView != null)
		{
			view = convertView;
			holder = (ViewHolder) view.getTag();
		} else
		{
			view = inflater.inflate(R.layout.coupon_item, null);
			holder = new ViewHolder();
			holder.couponIcon = (ImageView) view.findViewById(R.id.coupon_icon);
			holder.couponName = (TextView) view.findViewById(R.id.coupon_name);
			holder.couponDescription = (TextView) view.findViewById(R.id.coupon_description);
			holder.couponReceiveDate = (TextView) view.findViewById(R.id.coupon_receive_date);
			holder.couponUseDate = (TextView) view.findViewById(R.id.coupon_use_date);
			holder.couponOrderNumber = (TextView) view.findViewById(R.id.coupon_order_number);
			holder.couponValidDate = (TextView) view.findViewById(R.id.coupon_valid_date);
			holder.couponUseScope = (TextView) view.findViewById(R.id.coupon_use_scope);
		}
		PromotionCode coupon = couponList.get(position);
		view.setTag(holder);
		ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
		
		String productIcon = "http://m.rongji365.com/Themes/RongJi/Style/base/imgs/promotion/s_"+((int)coupon.getDiscount()) +".png";
		if (!TextUtils.isEmpty(productIcon))
		{
			ImageLoader.getInstance().displayImage(productIcon, holder.couponIcon);
		}
		holder.couponName.setText(coupon.getCouponName());
		holder.couponDescription.setText(coupon.getDescription());
		holder.couponReceiveDate.setText(context.getString(R.string.coupon_receive_time) + RongJiUtils.revertDate(coupon.getReceiveDate()));
		int status = coupon.getStatus();
		if(status == 0 || status == 2) //未使用订单和已过期订单不显示使用时间和订单号
		{
			holder.couponUseDate.setVisibility(View.GONE);
			holder.couponOrderNumber.setVisibility(View.GONE);
		}else
		{
			holder.couponUseDate.setVisibility(View.VISIBLE);
			holder.couponOrderNumber.setVisibility(View.VISIBLE);
			holder.couponUseDate.setText(context.getString(R.string.coupon_use_time) + RongJiUtils.revertDate(coupon.getStartDate()));
			holder.couponOrderNumber.setText(context.getString(R.string.coupon_order_num) + coupon.getOrderNumber());
		}
		
		holder.couponValidDate.setText(context.getString(R.string.coupon_valid_date) + RongJiUtils.revertDate(coupon.getEndTime()));
		ArrayList<PromotionCodeItem> itemList = coupon.getPromotionCodeItemList();
		String scope = "";
		if(itemList !=null && itemList.size() > 0)
		{
			for(int i = 0; i < itemList.size(); i ++)
			{
				PromotionCodeItem item = itemList.get(i);
				int usableType = item.getUsableType();
				int categoryId = item.getCategoryId();
				String productId = item.getProductId();
				int manufactoryId = item.getManufactoryId();
				if(usableType == 4 && categoryId > 0)
				{
					scope += context.getString(R.string.category_limit) + "<font color='#FF0000'>" + item.getCategoryName()+"</font>" + "<br>";
				}else if(usableType == 1 && !TextUtils.isEmpty(productId))
				{
					scope += context.getString(R.string.product_limit) + "<font color='#FF0000'>" + item.getProductName()+"</font>" + "<br>";
				}else if(usableType == 3 && manufactoryId > 0)
				{
					scope += context.getString(R.string.manufactory_limit) + item.getManufactoryName()+ "<br>";
				}else if(usableType == 2 && manufactoryId > 0 && categoryId > 0)
				{
					scope += context.getString(R.string.manufactory_category_limit) + item.getManufactoryName() + "-" + "<font color='#FF0000'>" + item.getCategoryName()+"</font>"+ "<br>";
				}
			}
			scope = scope.substring(0, scope.length() - 4);
			
			holder.couponUseScope.setText(Html.fromHtml(scope));
		}else
		{
			holder.couponUseScope.setText(context.getString(R.string.coupon_use_all));
		}
		return view;
	}

	private class ViewHolder
	{
		public ImageView couponIcon;
		public TextView couponName;
		public TextView couponDescription;
		public TextView couponReceiveDate;
		public TextView couponUseDate;
		public TextView couponOrderNumber;
		public TextView couponValidDate;
		public TextView couponUseScope;
	}
}
